package com.zy.mc_module.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Base64;

import com.zy.mc_module.mvp.base.BasePresenter;
import com.zy.mc_module.mvp.bean.BaseBean;
import com.zy.mc_module.utils.retrofit.RxObserver;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * Created on 19/2/27
 * 图片压缩工具类
 */
public class CompressUtils {
    private static String TAG = "CompressUtils";
    private static ArrayList<String> newPaths;//批量压缩后的图片路径

    /***
     * 图片批量压缩
     * @param context
     * @param paths  多图路径
     * @param onMyCompressListener  多图压缩完成的回调
     */
    public static void compressMore(Context context, List<String> paths, OnMyCompressListener
            onMyCompressListener) {
        newPaths = new ArrayList<>();
        Luban.with(context).load(paths).ignoreBy(188).setCompressListener(new OnCompressListener() {
            @Override
            public void onStart() {
                Logs.Log_e(TAG, "开始压缩 图片 ---->");
            }

            @Override
            public void onSuccess(File file) {
                Logs.Log_e(TAG, "图片压缩成功 " + file);
                newPaths.add(file.getPath());
                if (newPaths.size() == paths.size()) {
                    onMyCompressListener.complete(newPaths);
                }
            }

            @Override
            public void onError(Throwable e) {
                //压缩失败
                onMyCompressListener.onError(e);
                Logs.Log_e(TAG, "图片压缩失败 " + e.getMessage());
            }
        }).launch();
    }

    /***
     * 单张图片压缩并且上传
     * @param context
     * @param paths
     * @param onMyCompressListener
     */
    public static void compressUploadSing(Context context, String paths, OnMyCompressListener
            onMyCompressListener) {
        compressUploadMore(context, Arrays.asList(paths), true, onMyCompressListener);
    }

    /***
     * 图片批量压缩并且上传
     * @param context
     * @param paths  多图路径
     * @param isUpload  是否上传图片到服务器
     * @param onMyCompressListener  全部图片操作完成后的回调
     */
    public static <T> void compressUploadMore(Context context, List<T> paths, boolean isUpload, OnMyCompressListener
            onMyCompressListener) {
        if (!StringsUtils.isEmpty(paths)) {
            List<String> paths_str = new ArrayList<>();
            for (T path : paths) {
                if (path instanceof String) {
                    paths_str.add((String) path);
                }
            }
            newPaths = new ArrayList<>();
            onMyCompressListener.onStart();
            Luban.with(context).load(paths_str).ignoreBy(300).setCompressListener(new OnCompressListener() {
                @Override
                public void onStart() {
                    Logs.Log_e(TAG, "开始压缩 图片 ---->");
                }

                @Override
                public void onSuccess(File file) {
                    if (isUpload) {
                        ImageUpload.singleImg(file.getPath(), "-1", "0").subscribe(new RxObserver<BaseBean>(new
                                BasePresenter(), context) {
                            @Override
                            protected void onBaseNext(BaseBean baseBean) {
                                Logs.Log_e(TAG, "图片压缩并-上传成功  " + baseBean.data);
                                newPaths.add(baseBean.data.toString());
                                if (newPaths.size() == paths.size()) {
                                    onMyCompressListener.complete(newPaths);
                                }
                            }

                            @Override
                            protected void onBaseError(ThdException e) {
                                //上传失败
                                onMyCompressListener.onError(e);
                                Logs.Log_e(TAG, "图片上传失败 " + e.getMessage());
                            }
                        });
                    } else {
                        Logs.Log_e(TAG, "图片压缩成功  " + file.getPath());
                        newPaths.add(file.getPath());
                        if (newPaths.size() == paths.size()) {
                            onMyCompressListener.complete(newPaths);
                        }
                    }
                }

                @Override
                public void onError(Throwable e) {
                    //压缩失败
                    onMyCompressListener.onError(e);
                    Logs.Log_e(TAG, "图片压缩失败 " + e.getMessage());
                }
            }).launch();
        } else {
            //表示没有压缩并上传图片
            onMyCompressListener.complete(null);
        }
    }

    /***
     * 图片批量压缩并且上传
     * @param context
     * @param imgPath  压缩的图片
     * @param onMyCompressListener  全部图片操作完成后的回调
     */
    public static void compressImg(Context context, String imgPath, OnMyCompressListener
            onMyCompressListener) {
        if (null != imgPath) {
            newPaths = new ArrayList<>();
            onMyCompressListener.onStart();
            Luban.with(context).load(imgPath).ignoreBy(300).setCompressListener(new OnCompressListener
                    () {
                @Override
                public void onStart() {
                    Logs.Log_e(TAG, "开始压缩 图片 ---->");
                }

                @Override
                public void onSuccess(File file) {
                    Logs.Log_e(TAG, "图片压缩成功  " + file.getName() + "  --  " + file.getAbsolutePath());
                    ImageUpload.singleImg(file.getPath(), "-1", "0").subscribe(new RxObserver<BaseBean>(new
                            BasePresenter(), context) {
                        @Override
                        protected void onBaseNext(BaseBean baseBean) {
                            Logs.Log_e(TAG, "图片压缩并-上传成功  " + baseBean.data);
                            newPaths.add(baseBean.data.toString());
                            onMyCompressListener.complete(newPaths);
                        }

                        @Override
                        protected void onBaseError(ThdException e) {
                            //上传失败
                            onMyCompressListener.onError(e);
                            Logs.Log_e(TAG, "图片上传失败 " + e.getMessage());
                        }
                    });
                }

                @Override
                public void onError(Throwable e) {
                    //压缩失败
                    onMyCompressListener.onError(e);
                    Logs.Log_e(TAG, "图片压缩失败 " + e.getMessage());
                }
            }).launch();
        } else {
            //表示没有压缩并上传图片
            onMyCompressListener.complete(null);
        }
    }

    public interface OnMyCompressListener {
        //压缩开始
        void onStart();

        //压缩完成
        void complete(List<String> paths);

        //压缩失败
        void onError(Throwable throwable);
    }

    public static String bitmapToString(Bitmap bitmap) {
        //将Bitmap转换成字符串
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }
}
